//
/**
*
*File name:		LXGroup.swift
*History:		yoctech create on 2021/3/11       
*Description:
	
*/


import UIKit

public class LXGroup: NSObject {
    private var waitCount = 0
    private var completeBlock: (()->())
    
    public func enter() {
        waitCount += 1
        
        LXLog("[XGroup] 计数+1 值:\(waitCount)")
    }
    
    public override init() {
        completeBlock = {}
        super.init()
    }
    
    public init(notify: @escaping (()->())) {
        completeBlock = notify
        super.init()
    }
    
    public func leave() {
        if waitCount == 0 {
            return
        }
        waitCount = max(0, waitCount - 1)
        LXLog("[XGroup] 计数-1 值:\(waitCount)")
        if waitCount == 0 {
            finish()
        }
    }
    
    public func notify(_ block: @escaping (()->())) {
        completeBlock = block
    }
    
    private func finish() {
        LXLog("[XGroup] 执行completeBlock")
        self.completeBlock()
        self.completeBlock = {}
    }
    deinit {
        LXLog("[XGroup] deinit")

    }
}
